#include "QuestGiver.h"
#include "RaceQuest.h"
#include "BountyQuest.h"
#include "CollectQuest.h"
#include "AbstractEnemy.h"

QuestGiver::QuestGiver()
{
	radius = 10.0f;
}

QuestGiver::~QuestGiver()
{
	while(!Quests.empty())
	{
		SafeDelete(Quests.front());
		Quests.pop_back();
	}

	// Clean up
	SafeDelete(NPCModel);
}


void QuestGiver::AddToBatch(GraphicsInterface* gi)
{
	//Render the NPC model.
	gi->AddModelToBatch(NPCModel);
}

//function used to set quest givers position and to set up quests
void QuestGiver::Intialise(ID3D11Device* device, 
							XMFLOAT3 position, 
							XMFLOAT3 rotation, 
							XMFLOAT3 scale,
							Terrain* gameTerrain)
{
	// Load animating model, alloc memory and set the pointer for the game terrain
	NPCModel = new AnimatedModel();
	terrain = gameTerrain;

	// Load the animated model from file
	NPCModel->CreateAnimatedModelThroughAssimp(device, 
		std::string(NPC_MODEL_FILE_PATH), std::string(NPC_MODEL_TEXTURE_FILE_PATH), 
		false, true, false, false);
	
	// Set the models position on the y axis to be equal to the height of the terrain
	// it is currently occuping.
	position.y = terrain->GetHeight(position.x, position.z);

	// Set the models data in the world matrix
	NPCModel->SetWorldMatrix(position, rotation, scale);
	
	// Create a new material for the animated NPC model
	Material materialForNPCModel;
	materialForNPCModel.Diffuse.w = 1.0f;
	materialForNPCModel.Specular = XMFLOAT4(0,0,0,1);
	materialForNPCModel.Ambient = XMFLOAT4(0.2f, 0.2f, 0.2f, 1.0f);
	NPCModel->SetSubmodelsWithSameMaterial(materialForNPCModel);
}

// Simply calls the update function in the animated model class to play the animation
void QuestGiver::Update(float delta, bool shouldLoop, bool riggedPose)
{
	NPCModel->Update(delta, shouldLoop, riggedPose);
}

AbstractQuest* QuestGiver::GiveQuest()
{
	//loops through vector and returns an uncompleted quest
	//if there are no uncompleted quests then return null pointer
	for(std::vector<AbstractQuest*>::iterator it = Quests.begin(); it < Quests.end(); it++)
	{
		if((*it))
		{
			if(!(*it)->GetIsCompleted())
			{
				(*it)->Intialise();
				return (*it);
	
			}
		}
	}
	return 0;
}

void QuestGiver::AddQuest(AbstractQuest* quest)
{
	if(quest)
	{
		Quests.push_back(quest);
	}
}

XMFLOAT3 QuestGiver::GetPosition()
{
	//Ask the StaticModel instance where we are in world space. 
	return NPCModel->GetWorldPosition();
}

float QuestGiver::GetRadius()
{
	return radius;
}